/* *****************************************************************************************************
 * Copyright 2009. Dbwebx Group
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 * *****************************************************************************************************
 */

package org.dbwebx.persistence;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import java.io.Serializable;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;


/**
 * The entity represents database source code view node. <br>
 * Each project can be organized as hierarchical tree of nodes holding database
 * packages.
 *
 * @author Izzet_Mustafayev
 *
 */
@Entity
@Table(name = "t_source_nodes")
public class SourceNode implements Serializable {
    /**
     * serialVersionUID.
     */
    private static final long serialVersionUID = 1209358091324709L;

    /**
     * The source node identity.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long identity;

    /**
     * The source node name.
     */
    private String name;

    /**
     * The source node parent node.
     */
    @ManyToOne
    @JoinColumn(name = "parent_node_id", nullable = true)
    private SourceNode parent;

    /**
     * The source node last modifier user.
     */
    @ManyToOne
    @JoinColumn(name = "modifier_id")
    private User modifier;

    /**
     * The source node modification time.
     */
    @Column(name = "updated_on")
    @Temporal(TemporalType.TIMESTAMP)
    private Date updatedOn;

    /**
     * @return the identity
     */
    public Long getIdentity() {
        return identity;
    }

    /**
     * @param identity
     *            the identity to set
     */
    public void setIdentity(final Long identity) {
        this.identity = identity;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(final String name) {
        this.name = name;
    }

    /**
     * @return the parent
     */
    public SourceNode getParent() {
        return parent;
    }

    /**
     * @param parent
     *            the parent to set
     */
    public void setParent(final SourceNode parent) {
        this.parent = parent;
    }

    /**
     * @return the modifier
     */
    public User getModifier() {
        return modifier;
    }

    /**
     * @param modifier
     *            the modifier to set
     */
    public void setModifier(final User modifier) {
        this.modifier = modifier;
    }

    /**
     * @return the updatedOn
     */
    public Date getUpdatedOn() {
        return updatedOn;
    }

    /**
     * @param updatedOn
     *            the updatedOn to set
     */
    public void setUpdatedOn(final Date updatedOn) {
        this.updatedOn = updatedOn;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append(identity).append(name).toString();
    }

    @Override
    public boolean equals(final Object obj) {
        if (obj instanceof SourceNode == false) {
            return false;
        }

        if (this == obj) {
            return true;
        }

        SourceNode rhs = (SourceNode) obj;

        return new EqualsBuilder().append(identity, rhs.identity).append(name, rhs.name).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(11, 49).append(identity).append(name).toHashCode();
    }
}
